<?php

namespace App\Admin\Metrics\Examples;

use Dcat\Admin\Widgets\Metrics\Round;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Withdraw;

class WithDraws extends Round
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('代理提现统计');
        $this->chartLabels(['已打款', '待打款', '已拒绝']);
        $this->dropdown([
            '1' => '今日',
            '7' => '最近 7 天',
            '10' => '最近 10 天',
            '30' => '最近 30 天',
            '100' => '最近 100 天',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $days = $request->get('option')??1;
        $date = Carbon::today();
        if($days != 1 ){
            $date = Carbon::today()->addDays(1-$days);
        }

        $total = Withdraw::whereDate('created_at', '>=', $date)->sum('number');
        $finished = Withdraw::whereDate('created_at', '>=', $date)->where('state',1)->sum('number');
        $pending = Withdraw::whereDate('created_at', '>=', $date)->where('state',0)->sum('number');
        $rejected = Withdraw::whereDate('created_at', '>=', $date)->where('state',-1)->sum('number');

        // 卡片内容
        $this->withContent($finished, $pending,$rejected);

        // 图表数据
        if($total == 0 ){
            $this->withChart([0, 0, 0]);
        }
        else{
            $this->withChart([bcmul(bcdiv($finished,$total,2),100,2), bcmul(bcdiv($pending,$total,2),100,2), bcmul(bcdiv($rejected,$total,2),100,2)]);
        }

        // 总数
        $this->chartTotal('总金额', $total);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data,
        ]);
    }

    /**
     * 卡片内容.
     *
     * @param int $finished
     * @param int $pending
     * @param int $rejected
     *
     * @return $this
     */
    public function withContent($finished, $pending, $rejected)
    {
        return $this->content(
            <<<HTML
<div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
    <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-primary"></i>
              <span class="text-bold-600 ml-50">已打款</span>
          </div>
          <div class="product-result">
              <span>￥{$finished}</span>
          </div>
    </div>

    <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-warning"></i>
              <span class="text-bold-600 ml-50">待打款</span>
          </div>
          <div class="product-result">
              <span>￥{$pending}</span>
          </div>
    </div>

     <div class="chart-info d-flex justify-content-between mb-1">
          <div class="series-info d-flex align-items-center">
              <i class="fa fa-circle-o text-bold-700 text-danger"></i>
              <span class="text-bold-600 ml-50">已拒绝</span>
          </div>
          <div class="product-result">
              <span>￥{$rejected}</span>
          </div>
    </div>
</div>
HTML
        );
    }
}
